package cn.yusei.teamTribe.dao.relation;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import cn.yusei.teamTribe.dao.relation.po.RelationRequestPO;

@Component
public interface RelationRequestDAO {

	final String RELATION_REQUEST_TABLE = "e_relation_request";
	final String SELECT_FIELDS = " id, id1, id2, result, requestReason, responseReason, create_time ";
	
	@Insert({"insert into ", RELATION_REQUEST_TABLE, " (id1, id2, requestReason) values (#{id1}, #{id2}, #{requestReason})"})
	void insert(RelationRequestPO relationRequest);
	
	@Select({"select ", SELECT_FIELDS, " from ", RELATION_REQUEST_TABLE, " where id = #{id}"})
	RelationRequestPO selectById(long id);
	
	@Select({"select max(id) from ", RELATION_REQUEST_TABLE, " where id1 = #{id1} and id2 = #{id2}"})
	Long selectMaxId(@Param("id1") int id1, @Param("id2") int id2);
	
	@Select({"select ", SELECT_FIELDS, " from ", RELATION_REQUEST_TABLE, " where id1 = #{id1} and id2 = #{id2} and result = 0 limit 1"})
	RelationRequestPO exists(@Param("id1") int id1, @Param("id2") int  id2);
	
	@Update({"update ", RELATION_REQUEST_TABLE, " set result = #{result}, responseReason = #{responseReason} where id = #{id}"})
	void update(RelationRequestPO relationRequest);
	
}
